// let Client = require('ssh2-sftp-client');
// const path = require('path');
var ssh2 = require('ssh2').Client;

console.log('process.env.VUE_APP_PRO', process.env.VUE_APP_PRO);
console.log('process.env.VUE_APP_IP', process.env.VUE_APP_IP);
console.log('process.argv', process.argv);

let ip = '10.2.109.130';
let serverDir = '/root/manage-svg/mangage-svg-app';
// dev开发用;
if (process.env.VUE_APP_IP) {
  ip = process.env.VUE_APP_IP;
}
const account = {
  host: ip,
  port: '22',
  username: 'root',
  password: 'root',
};
const config = {
  // localFile: path.resolve("dist.zip"),
  serverDir,
  romotePath: 'dist.zip',
  account,
};

async function putFileFn(localPath, romotePath, host) {
  // let sftp = new Client();
  let conn = new ssh2();
  // romotePath = config.serverDir + config.romotePath;
  host && (config.account.host = host);
  console.log(config.account);

  conn
    .on('ready', function() {
      console.log('Client :: ready');
      conn.shell(function(err, stream) {
        if (err) throw err;
        stream
          .on('close', function() {
            console.log('Stream :: close');
            conn.end();
          })
          .on('data', function(data) {
            console.log('OUTPUT: ' + data);
          });
        // 需要服务端shell
        stream.end(
          'cd ' + config.serverDir + ' \n' + 'sh ./shell_init.sh \nexit\n'
        );
      });
    })
    .connect(config.account);
  //   sftp
  //     .connect(config.account)
  //     .then(async () => {
  //       return sftp.put(localPath, romotePath);
  //     })
  //     .then(async () => {
  //       console.log(localPath + "上传完成");
  //       sftp.end();

  //     })
  //     .catch(err => {
  //       console.log(err, "catch error");
  //     });
}

async function init() {
  await putFileFn(config.localFile, config.romotePath);
  // await putFileFn(config.localFile, config.romotePath, ip2);
}
init();
